package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.StringEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/15.
 *         <p/>
 *         添加商品名称
 */

public class GoodsAddNameFragment extends TheParentFragment {

    private AutoClickButton mBtn;
    private EditText mEtText;

    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_add_name;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsAddNameFragment)
            mActivity.setTitle(getString(R.string.goods_add_name));
    }

    @Override
    protected void init(View view) {
        mBtn = findView(view, R.id.confirm_btn);
        mBtn.setEnabled(false);
        mEtText = findView(view, R.id.set_rule_name);
        mBtn.bindEditText(mEtText);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.getString("name");
            if (name != null && !name.isEmpty())
                mEtText.setText(name);
        }
        ((BaseActivity) mActivity).hideSoftByEditViewIds(new int[]{R.id.set_rule_name});
    }

    @Override
    protected void loadDatas() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new StringEvent("create_name", mEtText.getText().toString()));
            }
        });
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }


    // 只允许字母、数字和汉字
    public static String stringFilter(String str) throws PatternSyntaxException {
        String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }


}
